Imports Microsoft.VisualBasic

Public Class LevelManager
    Public Function GetData(ByVal levelName As String) As DataRow
        Dim dat As New clsData
        Dim sql As New StringBuilder
        Dim params As New Dictionary(Of String, Object)

        sql.AppendFormat("SELECT * FROM ProductLevel ")
        sql.AppendFormat("WHERE 1=1 ")
        sql.AppendFormat("AND LevelName = @LevelName ")

        params.Add("LevelName", levelName)

        Return dat.GetRow(sql.ToString, params)
    End Function

    Public Function GetProductLevel() As DataTable
        Dim dat As New clsData
        Dim sql As New StringBuilder

        sql.AppendFormat("SELECT * FROM ProductLevel ")

        Return dat.GetData(sql.ToString)
    End Function

    Public Function GetLevelName2(ByVal Division As String, ByVal ViewPoint As String) As DataRow
        Return Me.GetLevelName2(Division, ViewPoint, 2)
    End Function

    Public Function GetLevelName2(ByVal Division As String, ByVal ViewPoint As String, ByVal CatagoryLevel As String) As DataRow
        Dim dat As New clsData
        Dim sql As New StringBuilder
        Dim params As New Dictionary(Of String, Object)

        sql.AppendFormat("  Select Top 1 HierarchyName as LevelName,LevelSequence as Sequence,Chain from  ")
        sql.AppendFormat("  ( ")
        sql.AppendFormat("    select top {0} A.HierarchyName,A.Sequence,B.Sequence as LevelSequence,B.Chain ", CatagoryLevel)
        sql.AppendFormat("    from MaintainLevel A join ProductLevel B on A.HierarchyName=B.LevelName ")
        sql.AppendFormat("    where A.ViewPoint=@ViewPoint ")
        sql.AppendFormat("    and A.Division=@Division and A.IsSelect='Y' order by A.Sequence ")
        sql.AppendFormat("  )A order by A.Sequence desc ")

        params.Add("ViewPoint", ViewPoint)
        params.Add("Division", Division)

        Return dat.GetRow(sql.ToString, params)
    End Function

    Public Function GetItemCode(ByVal Division As String, ByVal LevelName As String, ByVal SignatureText As String) As DataTable
        Dim dat As New clsData
        Dim sql As New StringBuilder

        sql.AppendFormat("  Select ItemCode from CodeCatagoryView ")
        sql.AppendFormat("  where Division='{0}' ", Division)
        sql.AppendFormat("  and LevelName='{0}' ", LevelName)
        sql.AppendFormat("  and Description='{0}' ", SignatureText.Replace("'", "''"))

        Return dat.GetData(sql.ToString)
    End Function

    Public Function GetItemCodeV2(ByVal Division As String, ByVal LevelName As String, ByVal SignatureText As String) As DataTable
        Dim dat As New clsData
        Dim sql As New StringBuilder

        sql.AppendFormat("exec GetItemCode '{0}','{1}','{2}' ", Division, LevelName, SignatureText.Replace("'", "''"))

        Return dat.GetData(sql.ToString)
    End Function

    Public Function GetItemCodes(ByVal Division As String, ByVal LevelName As String, ByVal SignatureText As String) As List(Of String)
        Dim dt As DataTable = GetItemCodeV2(Division, LevelName, SignatureText)
        Dim list As New List(Of String)

        For i As Integer = 0 To dt.Rows.Count - 1
            list.Add(dt.Rows(i)("ItemCode").ToString())
        Next

        Return list
    End Function
End Class
